//
//  ReadViewController.m
//  LO_MUSIC
//
//  Created by 徐强 on 16/2/24.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "ReadViewController.h"
#import "UrlRequestManager.h"
#define GETURL @"http://api2.pianke.me/read/columns"

#define POSTURL @"http://api2.pianke.me/ting/radio"

@interface ReadViewController ()

@end

@implementation ReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    //请求数据
//    [self post];
    //利用封装的代码请求数据
    [UrlRequestManager requestWith:GET URL:GETURL condition:nil SuccessBlock:^(id item) {
        NSLog(@"%@",item);
    } failBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)post {
    // POST
    NSURLSession *seesion = [NSURLSession sharedSession];
    // 字符串转utf-8 (ios7之后的)
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:POSTURL];
    NSString * urlString = [POSTURL stringByAddingPercentEncodingWithAllowedCharacters:set];
    // 字符串转utf-8 (被废弃的)
    //NSString *str = GETURL.UTF8String;
    
    // 设置request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    // 设置请求方式
    [request setHTTPMethod:@"POST"];
    // 设置请求体
    NSString *bodyStr = @"client=1&deviceid=63A94D37-33F9-40FF-9EBB-481182338873&auth=&version=3.0.2";
    NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    
    // 请求头 :
    NSURLSessionTask *task = [seesion dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        NSLog(@"%@",result);
    }];
    // 开启
    [task resume];
}

- (void)get {
    // GET
    NSURLSession *seesion = [NSURLSession sharedSession];
    // 字符串转utf-8 (ios7之后的)
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:GETURL];
    NSString * urlString = [GETURL stringByAddingPercentEncodingWithAllowedCharacters:set];
    // 字符串转utf-8 (被废弃的)
    //NSString *str = GETURL.UTF8String;
    
    // 设置request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    // 设置请求方式
    [request setHTTPMethod:@"GET"];
    //
    //[request setHTTPBody:nil];
    // 请求头 :
    NSURLSessionTask *task = [seesion dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        NSLog(@"%@",result);
    }];
    // 开启
    [task resume];
}


@end
